const { articleModels,subsetModels,fileModels } = require("../../models"); //导入数据库操作模块dbModels.js

  //获取分组
  exports.getSubset = async (req, res) => {
    try {
        let data = req.body;
        await subsetModels
          .getSubset(data.classify)
          .then(async(result) => {
            if(data.classify == 0 || data.classify == 1){
              let count = await articleModels.getArticleCount(data.state, -1, "",data.classify);
              let list =[];
              if(result.length>0){
                for (let i = 0; i < result.length; i++) {
                   let value = await articleModels.getArticleCount(data.state, result[i].id, "",data.classify);
                   list[i]={
                    id: result[i].id,
                    name: result[i].subset_name,
                    created_at: result[i].created_at,
                    value: value[0].count,
                   }
                }
              }
           
              res.send({
                code: 200,
                data: {count:count[0].count,list:list}
              });
            }else if(data.classify == 2){
                let count = await fileModels.getFileCount(-1);
                let list =[];
                if(result.length>0){
                  for (let i = 0; i < result.length; i++) {
                     let value = await fileModels.getFileCount(result[i].id);
                     list[i]={
                      id: result[i].id,
                      name: result[i].subset_name,
                      created_at: result[i].created_at,
                      value: value[0].count,
                     }
                  }
                }
                res.send({
                    code: 200,
                    data: {count:count[0].count,list:list}
                  });
            }
          }
        );
      } catch (error) {
        res.send({
          code: 400,
          data: error,
        });
      }
  }
  
//新建分组
exports.addSubset = async (req, res) => {
    try {
        let data = req.body;
        await subsetModels
         .addSubset(data.value)
         .then((result) => {
            res.send({
              code: 200,
              data: result.insertId
            });
          });
      } catch (error) {
        res.send({
          code: 400,
          data: error,
        })
      }
}

//修改分组名称
exports.updateSubset = async (req, res) => {
    try {
        let data = req.body;
        await subsetModels
         .updateSubset(data.subsetId,data.subset_name)
         .then((result) => {
            res.send({
              code: 200,
              data: result
            });
          });
      } catch (error) {
        res.send({
          code: 400,
          data: error,
        })
      }
}

//删除分组
exports.deleteSubset = async (req, res) => {
    try {
        let data = req.body;
        await subsetModels
        .deleteSubset(data.id)
        .then((result) => {
            res.send({
              code: 200,
              data: result
            });
          });
      } catch (error) {
        res.send({
          code: 400,
          data: error,
        })
      }
}